<?php
/*
 * 前台导购显示类
 * @package    auto
 * @author     YuNian
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 9ae087211b63138bee5e535132b99832f35dba2f $
 */

class AutoOwners {

	private static $_types;
	private static $_table;
	private static $_tableData;


	public static function category() {
		if (!empty(self::$_types)) {
			return self::$_types;
		} else {
			self::$_types = PCache::read('category-32.php');
			return self::$_types;
		}
	}

	private static function _getTable() {
		if (!empty(self::$_table)) {
			return self::$_table;
		} else {
			self::$_table = Table::name(Table::NAME_OWNERS);
			return self::$_table;
		}
	}

	private static function _getTableData() {
		if (!empty(self::$_tableData)) {
			return self::$_tableData;
		} else {
			self::$_tableData = Table::name(Table::NAME_OWNERS_DATA);
			return self::$_tableData;
		}
	}

	/**
	 * 根据车系获取
	 * @param     $carid
	 * @param int $limit
	 * @return array
	 */
	public static function byCar($carid, $limit=10) {
		global $db;
		$sql = "select * from ".self::_getTable()." where arcid = $carid  order by addtime DESC limit $limit";
		$info = $db->getAll($sql);
		if (count($info) == 10) {
			return $info;
		} else {
			$left = $limit - count($info);
			$brandId = AutoRank::getArbidByArcid($carid);
			$sql = "select * from ".self::_getTable()." where arbid = $brandId  order by addtime DESC limit $left";
			$leftInfo = $db->getAll($sql);
			$infos = array_merge($info, $leftInfo);
			return __($infos)->uniq('title');
		}

	}

	/**
	 * 根据用户名获取头像
	 * @param   $username
	 * @return $string
	 */
	public static function byPhoto($username) {
		global $db;
		$MOD = PCache::read('module-2.php');
		require DT_ROOT.'/api/uc.inc.php';
		unset($MOD);
		$user = $db->getOne("SELECT userid from xd_member WHERE username = '".$username."'");
		return UC_API.'/avatar.php?uid='.$user['userid'];
	}
}